import request from '@/router/axios';
import Mock from 'mockjs';

//ids
// var ids = Mock.mock({"id|5-20":1000});

let _list= [
    {
        name:'售货员',
        desc:'售货员',
        enabled:true,
        updateTime:'2021-04-13 10:33:13',
        addTime:'2021-04-13 10:33:10',
        roles:[20,21],
        id:1
    },
    {
        name:'商户管理员',
        desc:'品牌商户管理员角色',
        enabled:true,
        updateTime:'2020-10-23 10:07:58',
        addTime:'2020-10-23 10:07:58',
        roles:[10,11,12,20,21,22,50,53],
        id:2
    },
    {
        name:'超级管理员',
        desc:'所有模块的权限',
        enabled:true,
        updateTime:'2021-01-01 00:00:00',
        addTime:'2021-01-01 00:00:00',
        roles:[10,11,12,20,21,22,30,31,32,40,41,42,50,51,52,53],
        id:3
    },
    {
        name:'推广管理员',
        desc:'只有推广模块的操作权限',
        enabled:true,
        updateTime:'2021-04-13 10:33:13',
        addTime:'2021-04-13 10:33:10',
        roles:[30,31,32,],
        id:4
    }
];

let RolesList = [
    {
        label:'用户管理',
        value:10,
        children:[
            {
                label:'用户数据管理',
                value:11,
            },
            {
                label:'佣金管理',
                value:12,
            }
        ]
    },
    {
        label:'商品管理',
        value:20,
        children:[
            {
                label:'商品列表',
                value:21,
            },
            {
                label:'商品评论',
                value:22,
            }
        ]
    },
    {
        label:'推广管理',
        value:30,
        children:[
            {
                label:'广告管理',
                value:31,
            },
            {
                label:'团购规则',
                value:32,
            }
        ]
    },
    {
        label:'系统管理',
        value:40,
        children:[
            {
                label:'管理员',
                value:41,
            },
            {
                label:'角色管理',
                value:42,
            }
        ]
    },
    {
        label:'统计',
        value:50,
        children:[
            {
                label:'用户统计',
                value:51,
            },
            {
                label:'订单统计',
                value:52,
            },
            {
                label:'商品统计',
                value:53,
            }
        ]
    },
]

//获取时间
function getDate() {
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth();//得到月份
    var date = now.getDate();//得到日期
    var day = now.getDay();//得到周几
    var hour = now.getHours();//得到小时
    var minu = now.getMinutes();//得到分钟
    var sec = now.getSeconds();//得到秒
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var time = "";
    time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
    return time;
}

// 获取角色列表
export const rolesList = (data) =>request({
    url:'/rolesList',
    method:'get',
    data
})
Mock.mock(/\/rolesList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

// 获取菜单权限列表
export const roleIdList = () =>request({
    url:'/roleIdList',
    method:'get'
})
Mock.mock(/\/roleIdList/,config=>{
    return {
        data:{
            RolesList
        },
        meta:{
            msg:'请求成功',
            status:200
        }
    }
})

//添加角色
export const addRoles = (data) =>request({
    url:'/addRoles',
    mothed:'post',
    data
});
Mock.mock(/\/addRoles/,config=>{
    var ids = Mock.mock({"id|5-20":1000});
    let datas = JSON.parse(config.body)
    let {name,roles} = datas;
    if(name && roles){
        _list.unshift({
            name,
            desc:datas.desc?datas.desc:'',
            enabled:true,
            updateTime:getDate(),
            addTime:getDate(),
            roles,
            id:ids.id
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除角色
export const detRoles = (data) =>request({
    url:'/detRoles',
    mothed:'delete',
    data
})
Mock.mock(/\/detRoles/,config=>{
    let {id} = JSON.parse(config.body);
    if(id){
        _list.splice(_list.findIndex(item=>item.id==id),1)
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//查询角色 根据角色名
export const queryRoles = (data) =>request({
    url:'/queryRoles',
    mothed:'get',
    data
})
Mock.mock(/\/queryRoles/,config=>{
    let {name} = JSON.parse(config.body);
    let arr = [];
    if(name){
        let arr = _list.filter((item,index)=>item.name.indexOf(name)!=-1);
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 修改角色描述
export const editRoles = (data) =>request({
    url:'/editRoles',
    mothed:'put',
    data
})
Mock.mock(/\/editRoles/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.name = datas.name?datas.name:item.name;
                item.desc= datas.desc?datas.desc:item.desc;
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//角色授权
export const authRoles = (data) =>request({
    url:'/authRoles',
    mothed:'put',
    data
})
Mock.mock(/\/authRoles/,config=>{
    let datas = JSON.parse(config.body);
    let {id,roles} = datas;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.roles = roles;
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})
